package com.book.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.book.mapper.AdminMapper;
import com.book.pojo.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;

@Service
@Transactional
public class AdminService extends ServiceImpl<AdminMapper, Admin> {

    @Autowired
    private AdminMapper adminMapper;

    //登录验证
    public String loginCheck(Admin loginAdmin, HttpSession session){

        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", loginAdmin.getUsername()); //查询用户
        Admin admin = adminMapper.selectOne(queryWrapper);

        if (admin == null){
            return "101"; //管理员不存在
        }else {
            //判断密码是否正确
            if(loginAdmin.getPassword().equals(admin.getPassword())){
                session.setAttribute("admin", admin);
                return "100"; //密码正确
            }else {
                return "102"; //密码不正确
            }
        }
    }
}
